﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Maps.MapControl;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using SL4_NAV_ProjectiCON.Model;
using SL4_NAV_ProjectiCON.Providers;

namespace SL4_NAV_ProjectiCON.Views
{
    public partial class Estimate : Page
    {
        private Random _random = new Random();
        List<DataGridRow> Rows = new List<DataGridRow>();
       
        public Estimate()
        {
            InitializeComponent();

            dataGrid1.LoadingRow += (sender, e) =>
            {
                var oldRows = Rows.Where(w => w.DataContext.Equals(e.Row.DataContext)).ToList();
                foreach (var row in oldRows)
                {
                    Rows.Remove(row);
                }
                Rows.Add(e.Row);
            };


           

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            Clear();
            SL4_NAV_ProjectiCON.DataService.DataServiceClient objCust = new SL4_NAV_ProjectiCON.DataService.DataServiceClient();
            objCust.GetCustomersCompleted += new EventHandler<SL4_NAV_ProjectiCON.DataService.GetCustomersCompletedEventArgs>(GetCustomersCompleted);
            objCust.GetCustomersAsync();

             //create a PageCollection view from our sales data 
            //PagedCollectionView collectionView = new PagedCollectionView(dataGrid1.ItemsSource);
            //// set the page size 
            //collectionView.PageSize = 10;
            //// set the data context to the page collection views 
            //dgCustomerPager.DataContext = collectionView;
        } 

        public void Load()
        {
            Clear();
            SL4_NAV_ProjectiCON.DataService.DataServiceClient objCust = new SL4_NAV_ProjectiCON.DataService.DataServiceClient();
            objCust.GetCustomersCompleted += new EventHandler<SL4_NAV_ProjectiCON.DataService.GetCustomersCompletedEventArgs>(GetCustomersCompleted);
            objCust.GetCustomersAsync();

        }

        public void ReLoad(String objCuststr)
        {
            Clear();
            SL4_NAV_ProjectiCON.DataService.DataServiceClient objCust1 = new SL4_NAV_ProjectiCON.DataService.DataServiceClient();
            objCust1.GetCustomersBynameCompleted += new EventHandler<SL4_NAV_ProjectiCON.DataService.GetCustomersBynameCompletedEventArgs>(GetCustomersByNameCompleted);
            objCust1.GetCustomersBynameAsync(objCuststr);
        }

        void GetCustomersCompleted(object sender, SL4_NAV_ProjectiCON.DataService.GetCustomersCompletedEventArgs e)
        {
            //MapLayer PushPinLayer = null;
            //PushPinLayer = new MapLayer();
            //PushPinLayer.Name = "PushPinLayer";
            //MyMap.Children.Add(PushPinLayer);

            dataGrid1.ItemsSource = e.Result;


            //PagedCollectionView collectionView = new PagedCollectionView(e.Result);
            //// set the page size 
            //collectionView.PageSize = 10;
            //// set the data context to the page collection views 
            //this.DataContext = collectionView;

            dataGrid1.SelectionChanged += new SelectionChangedEventHandler(dataGrid1_SelectionChanged);
            foreach (SL4_NAV_ProjectiCON.DataService.Customer C in e.Result)
            {
                CustomerPushpin pin;
                Location d = new Location(Convert.ToDouble(C.Latitude), Convert.ToDouble(C.Longitude));
                if (C.CustomerId > 10050)
                    pin = new CustomerPushpin(Colors.Blue);
                else
                    pin = new CustomerPushpin(Colors.Red);

                pin.Location = d;
                pin.Name = C.CustomerId.ToString();
                pin.CustomerId = C.CustomerId.ToString();
                pin.CustomerName = C.CustomerName.ToString();
                ScaleTransform st = new ScaleTransform();
                st.ScaleX = 0.25;
                st.ScaleY = 0.25;
                st.CenterX = (pin as FrameworkElement).Height / 2;
                st.CenterY = (pin as FrameworkElement).Height / 2;

                pin.RenderTransform = st;
                pin.MouseEnter += new MouseEventHandler(point_MouseEnter);
                pin.MouseLeave += new MouseEventHandler(point_MouseLeave);
                PushPinLayer.AddChild(pin, d);
            }
        }

       
        void GetCustomersByNameCompleted(object sender, SL4_NAV_ProjectiCON.DataService.GetCustomersBynameCompletedEventArgs e)
        {
           dataGrid1.ItemsSource = e.Result;

        }

        public void Clear()
        {
            List<UIElement> elementsToRemove = new List<UIElement>();
            List<UIElement> pushpinToRemove = new List<UIElement>();
            foreach (UIElement element in MyMap.Children)
            {
                if (element.GetType() == typeof(MapLayer))
                {
                    MapLayer Lay = (MapLayer)element;
                    if (Lay.Name == "PushPinLayer")
                    {
                        foreach (UIElement p in Lay.Children)
                        {
                            if (p.GetType() == typeof(CustomerPushpin))
                            {
                                pushpinToRemove.Add(p);
                            }
                        }
                        foreach (UIElement pin in pushpinToRemove)
                        {
                            Lay.Children.Remove(pin);
                        }
                    }
                    elementsToRemove.Add(Lay);
                }
            }
            //foreach (UIElement e in elementsToRemove)
            //{
            //    MyMap.Children.Remove(e);
            //}
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            Clear();
        }


        void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //to get the current row binding value
            SL4_NAV_ProjectiCON.DataService.Customer currentRow = (SL4_NAV_ProjectiCON.DataService.Customer)dataGrid1.SelectedItem;
            ////to read the currentRow
            string CustomerName = currentRow.CustomerName;  
            ProcessEstimates(CustomerName);

        }

        void ProcessEstimates(string containerName)
        {

            ObservableCollection<Westimdt> lst_westimdt;  
            lst_westimdt = WestimdtProviders.GetWestimDtDetails();
            if (containerName == "Gabriel")
            {
                containerName = "CMUG543216";
            }
            if (containerName == "Aaron")
            {
                containerName = "CRXU123456";
            }

            MessageBox.Show(containerName);

           
            foreach (var it in lst_westimdt)
            {
                if (this.RB4N.Content.ToString() == it.WD_LOCATION)
                {
                    MessageBox.Show(it.WD_LINE);
                    MessageBox.Show(it.WD_LOCATION);
                    MessageBox.Show(this.RB4N.Content.ToString());
                    Border border = new Border();
                    border.Width = 60;
                    border.Height = 60;
                    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                    RB4N.Content = border;

                }

                if (this.UX3N.Content.ToString() == it.WD_LOCATION)
                {
                    Border border = new Border();
                    border.Width = 60;
                    border.Height = 60;
                    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                    UX3N.Content = border;
                    //TR2N.Content= "PANEL, PATCH,";
                    //output.Text = "UNDERSTRUCTURE PANEL- DAMAGE : SCRATCHES, REPAIR- WASHING & CLEANING";
                }

                if (this.RB4N.Content.ToString() == "RB4N")
                {

                    //TR2N.Content= "PANEL, PATCH,";
                    // output.Text = "PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
                }
                if (this.RH5N.Content.ToString() == "RH5N")
                {
                    Border border = new Border();
                    border.Width = 60;
                    border.Height = 60;
                    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                    RH5N.Content = border;
                    //TR2N.Content= "PANEL, PATCH,";
                    //output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
                }

                if (this.DB2N.Content.ToString() == "DB2N")
                {
                    Border border = new Border();
                    border.Width = 60;
                    border.Height = 60;
                    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                    DB2N.Content = border;
                    //TR2N.Content= "PANEL, PATCH,";
                    //output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
                }
            }

           
            
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        void point_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //Show a popup with data about that point
            CustomerPushpin pin = sender as CustomerPushpin;
            if (pin != null)
            {
                MapLayer.SetPosition(ContentPopup, pin.Location);
                MapLayer.SetPositionOffset(ContentPopup, new Point(0, -60));



                string contentString = pin.CustomerId.ToString() + "   " + pin.CustomerName.ToString();
                //Remove tags from the string
                Regex regex = new Regex("<(.|\n)*?>");
                contentString = regex.Replace(contentString, string.Empty);
                ContentPopupText.Text = contentString;
                ContentPopup.Visibility = Visibility.Visible;

                ReLoad(pin.CustomerName.ToString());


            }
        }

        void point_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //Hide the popup
            CustomerPushpin point = sender as CustomerPushpin;
            ContentPopup.Visibility = Visibility.Collapsed;
        }

        private void ContentPopup_MouseEnter(object sender, MouseEventArgs e)
        {
            //Show the popup
            (sender as Canvas).Visibility = Visibility.Visible;
            Canvas.SetZIndex((sender as Canvas), 10);
        }

        private void ContentPopup_MouseLeave(object sender, MouseEventArgs e)
        {
            ////Show the popup
            (sender as Canvas).Visibility = Visibility.Collapsed;
        }

        private void OnButtonTransformInteractionControlClick(object sender, EventArgs e)
        {
            //output.Text = "Button container clicked!";
            this.TR2N.Content = "TR2N";

            if (this.TR2N.Content.ToString() == "TR2N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                TR2N.Content = border;
                //output.Text = "PANEL - DAMAGE : PATCH , REPAIR- REPLACE";
            }
        }

        private void OnBorderTransformInteractionControlClick(object sender, EventArgs e)
        {
            //output.Text = "Border container clicked!";

            if (this.RB4N.Content.ToString() == "RB4N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                RB4N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
                //output.Text = "PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            }
            if (this.UX3N.Content.ToString() == "UX3N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                UX3N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
               // output.Text = "UNDERSTRUCTURE PANEL- DAMAGE : SCRATCHES, REPAIR- WASHING & CLEANING";
            }
        }

        private void OnButtonTransformInteractionControlClick_Test2(object sender, EventArgs e)
        {
            //output.Text = "Test 2- Button container clicked!";

            if (this.RH5N.Content.ToString() == "RH5N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                RH5N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
                //output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
            }

            if (this.DB2N.Content.ToString() == "DB2N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                DB2N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
                //output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            }
        }

        private void OnButtonTransformInteractionControlClick_Text(object sender, EventArgs e)
        {
            if (this.RH5N.Content.ToString() == "RH5N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                RH5N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
                //output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
            }

            if (this.DB2N.Content.ToString() == "DB2N")
            {
                Border border = new Border();
                border.Width = 60;
                border.Height = 60;
                border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
                DB2N.Content = border;
                //TR2N.Content= "PANEL, PATCH,";
                //output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            }
        }

        private void OnAddButtonClick(object sender, RoutedEventArgs e)
        {
            TransformInteraction.TransformInteractionControl transformControl = new TransformInteraction.TransformInteractionControl();
            transformControl.HorizontalAlignment = HorizontalAlignment.Center;
            transformControl.VerticalAlignment = VerticalAlignment.Center;
            Border border = new Border();
            border.Width = 50;
            border.Height = 50;
            border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            transformControl.Content = border;
            LayoutRoot.Children.Add(transformControl);
        }

        private void MyMap_Loaded(object sender, RoutedEventArgs e)
        {

        }
          
    }
    public class CustomerPushpin1 : Pushpin
    {
        public string CustomerName { get; set; }
        public string CustomerId { get; set; }
        public string CustomerAddress { get; set; }
        public CustomerPushpin1(Color Bg)
        {
            this.Name = Guid.NewGuid().ToString();
            SolidColorBrush scb = new SolidColorBrush(Bg);
            this.Background = scb;
        }
    }

    public class Customer1
    {
        public string CustomerName { get; set; }
        public string CustomerId { get; set; }

    }
}
